//
//  FSLoginView.m
//  cocoapodText
//
//  Created by apple on 16/8/10.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShiPei.h"
#import "FSLoginView.h"
#import <Foundation/Foundation.h>

@interface FSLoginView()<UITextFieldDelegate>
@end

@implementation FSLoginView

/**
 *  初始化视图属性
 *
 *  @param frame 视图frame
 *
 *  @return self
 */
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //定制View
        self.backgroundColor = [UIColor clearColor];
        self.userInteractionEnabled = YES; //设置为NO后，不再响应touch方法
        self.multipleTouchEnabled = YES;
        //控制子视图不能超出父视图的范围
        self.clipsToBounds = YES;
        //添加子视图
        [self subView];
    }
    return self;
}

//子视图懒加载
-(void)subView{
    self.loginAllBackView = [self GetLoginView];
}

/**
 * @brief 初始化登录view.
 */
-(UIView *)GetLoginView{
    CGFloat loginViewW=490;
    CGFloat loginViewH=330;
    CGFloat editBGW=400;
    CGFloat editBGH=45;
    CGFloat buttonBGW=400;
    CGFloat buttonBGH=45;
    ///底部LoginView
    UIView* loginAllView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [ShiPei CGFloatMakeScale:loginViewW], [ShiPei CGFloatMakeScale:loginViewH])];
    [loginAllView setBackgroundColor:[UIColor whiteColor]];
    //将图层的边框设置为圆脚
    [loginAllView.layer setCornerRadius:8];
    [loginAllView.layer setMasksToBounds:YES];
    //给图层添加一个有色边框
    [loginAllView.layer setBorderColor:[FUIColorFromRGB(0xffffff) CGColor]];
    [loginAllView.layer setBorderWidth:5];
    
    ///账户输入框
    [loginAllView addSubview:[self GetLoginEditTextWithRect:[ShiPei CGRectMakeScale:(loginViewW-editBGW)/2: 60: editBGW: editBGH] edit:@"loginIdView" image:@"fs_login_id.png" hint:@"用户名"]];
    ///密码输入框
    [loginAllView addSubview:[self GetLoginEditTextWithRect:[ShiPei CGRectMakeScale:(loginViewW-editBGW)/2: 130: editBGW: editBGH] edit:@"loginPassView" image:@"fs_login_pass.png" hint:@"密码"]];
    ///密码相关
    [loginAllView addSubview:[self GetOtherPassRelevantViewWithRect:[ShiPei CGRectMakeScale:(loginViewW-buttonBGW)/2: 195: buttonBGW: 25]]];
    ///登录按钮
    [loginAllView addSubview:[self GetLoginButtonWithRect:[ShiPei CGRectMakeScale:(loginViewW-buttonBGW)/2: 240: buttonBGW: buttonBGH]]];
    
    [self addSubview:loginAllView];
    return loginAllView;
}

-(UIView *)GetLoginEditTextWithRect:(CGRect)rootBGRect edit:(NSString *)editTextViewName image:(NSString *)imageName hint:(NSString *)hintString{
    CGFloat editBGW=rootBGRect.size.width;
    CGFloat editBGH=rootBGRect.size.height;
    UIView *loginEditTextBGView=[[UIView alloc]initWithFrame:rootBGRect];
    //边框
    [loginEditTextBGView.layer setBorderWidth:1];
    [loginEditTextBGView.layer setBorderColor:[FUIColorFromRGB(0xdfe2e6) CGColor]];
    
    //图片
    CGFloat imageWH=20;
    UIImageView *editImageView=[[UIImageView alloc]initWithFrame:[ShiPei CGRectMakeScale:12: (editBGH-imageWH)/2:imageWH: imageWH]];
    [editImageView setImage:[UIImage imageNamed:imageName]];
    [loginEditTextBGView addSubview:editImageView];
    
    //编辑框
    CGFloat editW=editBGW-imageWH-20-12-10;
    CGFloat editH=editBGH;
    UITextField * editTextView=[[UITextField alloc]initWithFrame:[ShiPei CGRectMakeScale:imageWH+20+12: 0: editW: editH]];
    editTextView.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    editTextView.placeholder=hintString;
    editTextView.backgroundColor=[UIColor whiteColor]; //背景色
    editTextView.font=[UIFont fontWithName:@"Arial" size:18.0]; //设置字体名字和字体大小;
    editTextView.returnKeyType = UIReturnKeyDefault;//return键的类型
    editTextView.keyboardType = UIKeyboardTypeDefault;//键盘类型
    editTextView.textAlignment = NSTextAlignmentLeft; //文本显示的位置默认为居左
    editTextView.textColor = FUIColorFromRGB(0x333333);
    editTextView.text = @"";//设置显示的文本内容//设置委托
    
    //在弹出的键盘上面加一个view来放置退出键盘的Done按钮
    UIToolbar * topView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    [topView setBarStyle:UIBarStyleDefault];
    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
    NSArray * buttonsArray = [NSArray arrayWithObjects:btnSpace, doneButton, nil];
    [topView setItems:buttonsArray];
    [editTextView setInputAccessoryView:topView];
    
    [loginEditTextBGView addSubview:editTextView];
    if([editTextViewName isEqualToString:@"loginIdView"]){
        self.loginIdView=editTextView;
        [editTextView setText:[CommonTool getValueFromKey:@"username" defaultValue:@""]];
    }else{
        self.loginPassView=editTextView;
        self.loginPassView.secureTextEntry = YES;
        [editTextView setText:[CommonTool getValueFromKey:@"userpass" defaultValue:@""]];
    }
    return loginEditTextBGView;
}

-(UIView *)GetOtherPassRelevantViewWithRect:(CGRect) frame{
    CGFloat rootBGW=frame.size.width;
    CGFloat rootBGH=frame.size.height;
    CGFloat labelW=80;
    CGFloat labelH=rootBGH;
    UIView *PassRelevantBGView=[[UIView alloc]initWithFrame:frame];
    CGFloat imageWH=20;
    ///记住密码
    UIImageView *switchSavePassImageView=[[UIImageView alloc]initWithFrame:[ShiPei CGRectMakeScale:10 : (rootBGH-imageWH):imageWH :imageWH]];
    NSString *issavepass=[CommonTool getValueFromKey:@"is_save_pass" defaultValue:@"0"];
    if ([issavepass isEqualToString:@"1"]) {
        [switchSavePassImageView setImage:[UIImage imageNamed:@"login_check_box_1.png"]];
    }else{
        [switchSavePassImageView setImage:[UIImage imageNamed:@"login_check_box_0.png"]];
    }
    switchSavePassImageView.userInteractionEnabled=YES;
    UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(switchSavePass)];
    [switchSavePassImageView addGestureRecognizer:singleTap];
    [PassRelevantBGView addSubview:switchSavePassImageView];
    self.switchSavePassImageView=switchSavePassImageView;
    
    UILabel *switchSavePassLabel=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:40 :0 :labelW :labelH]];
    [switchSavePassLabel setText:@"记住密码"];
    [switchSavePassLabel setTextColor:FUIColorFromRGB(0x999999)];
    [switchSavePassLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:30]]];
    [switchSavePassLabel setTextAlignment:NSTextAlignmentLeft];
    [PassRelevantBGView addSubview:switchSavePassLabel];
    ///忘记密码
    UILabel *forgetPassLabel=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:rootBGW-labelW :0 :labelW :labelH]];
    [forgetPassLabel setText:@"忘记密码?"];
    [forgetPassLabel setTextColor:FUIColorFromRGB(0x666666)];
    [forgetPassLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:30]]];
    [forgetPassLabel setTextAlignment:NSTextAlignmentRight];
    switchSavePassImageView.userInteractionEnabled=YES;
    UITapGestureRecognizer *forgetPass =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(forget)];
    [forgetPassLabel addGestureRecognizer:forgetPass];
    //[PassRelevantBGView addSubview:forgetPassLabel];
    //self.forgetPassView=forgetPassLabel;
    
    UIImageView *underLineImageView=[[UIImageView alloc]initWithFrame:[ShiPei CGRectMakeScale:rootBGW-labelW :rootBGH-1 :labelW :1]];
    [underLineImageView setBackgroundColor:FUIColorFromRGB(0x666666)];
    //[PassRelevantBGView addSubview:underLineImageView];
    return  PassRelevantBGView;
}

-(UIButton *)GetLoginButtonWithRect:(CGRect)buttonRect{
    ///登录按钮
    UIButton *loginButtonView=[[UIButton alloc]initWithFrame:buttonRect];
    //颜色
    [loginButtonView setBackgroundImage:[CommonTool createImageWithColor:FUIColorFromRGB(0x3388cb) Frame:[ShiPei CGRectMakeScale:0 :0 :50 :25]] forState:UIControlStateNormal];
    [loginButtonView setBackgroundImage:[CommonTool createImageWithColor:FUIColorFromRGB(0x639ed7)  Frame:[ShiPei CGRectMakeScale:0 :0 :50 :25]] forState:UIControlStateHighlighted];
    //[loginButtonView setBackgroundColor:FUIColorFromRGB(0x3388cb)];
    //圆角
    [loginButtonView.layer setBorderWidth:1];
    [loginButtonView.layer setBorderColor:[FUIColorFromRGB(0x3388cb) CGColor]];
    [loginButtonView.layer setCornerRadius:5];
    [loginButtonView.layer setMasksToBounds:YES];
    //文字
    [loginButtonView.titleLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:38]]];
    [loginButtonView.titleLabel setTextColor:[UIColor whiteColor]];
    [loginButtonView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButtonView setTitle:@"登录" forState:UIControlStateNormal];
    [loginButtonView addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    self.loginButtonView=loginButtonView;
    return loginButtonView;
}

//点击忘记密码
-(void)forget{
    if([_delegate respondsToSelector:@selector(onForget:)]){
        [_delegate onForget:self];
    }
}
//切换是否记住密码
-(void)switchSavePass{
    if([_delegate respondsToSelector:@selector(onSwitchSavePass:)]){
        [_delegate onSwitchSavePass:self];
    }
}
//登录
-(void)login{
    if([_delegate respondsToSelector:@selector(onLogin:)]){
        [_delegate onLogin:self];
    }
}

-(void)refreshView{
    NSString *is_save_pass=[CommonTool getValueFromKey:@"is_save_pass" defaultValue:@"1"];
    if (![is_save_pass isEqualToString:@"1"]) {
        [_loginPassView setText:@""];
    }
}

//关闭键盘
-(void) dismissKeyBoard{
    [_loginIdView resignFirstResponder];
    [_loginPassView resignFirstResponder];
}

@end
